import 'package:get/get.dart';
import 'package:sgy_flutter/ble/ble_manager.dart';
import 'package:sgy_flutter/ble/errors.dart';
import 'package:sgy_flutter/extensions/get.dart';
import 'package:sgy_flutter/widget/app_dialog.dart';

class PowerOffsetLogic extends GetxController {
  Rxn<int> powerOffset = Rxn();

  var loading = false.obs;

  var success = false.obs;

  @override
  void onReady() async{
    powerOffset(bleManager.powerOffset.value);
    super.onReady();
  }

  Future setPowerOffset()async{

    try{
      loading(true);
      var hasOneSuccess = false;

      try{
        if (bleManager.isReady.value == true){
          await bleManager.setPowerOffset(powerOffset.value ?? 1000);
          bleManager.refreshPowerOffset();
          hasOneSuccess = true;
        }
      }on EmptyWriteCharacteristicError catch(e){
        printError(info: "$e");
        _showNotSupport(bleManager);
      }catch(e){

      }
      try{
        if (rightBleManager?.isReady.value == true){
          await rightBleManager?.setPowerOffset(powerOffset.value ?? 1000);
          rightBleManager?.refreshPowerOffset();
          hasOneSuccess = true;
        }
      }on EmptyWriteCharacteristicError catch(e){
        printError(info: "$e");
        _showNotSupport(rightBleManager);
      }catch(e){

      }
      printInfo(info: "hasOneSuccess:$hasOneSuccess");
      if (!hasOneSuccess){
        if (await bleManager.ensureDeviceConnected()){
          // await setPowerOffset();
        }
      }else{

        success(true);
      }

    }catch(e){
      print("$e");
    }finally{
      loading(false);
    }
  }

  void _showNotSupport(BleManager? manager){
    final firmware = manager?.deviceModel?.firmwareNumber ?? 0;
    if (manager?.deviceModel?.name.startsWith("DLS") == true){
      if (firmware <= 4.4){
        Get.dialog(AppDialog(title: "提示".tr, message: "请先升级固件".tr, submit: "确定".tr), barrierDismissible: false);
      }else{
        Get.dialog(AppDialog(title: "提示".tr, message: "请先移除充电线并重新连接".tr, submit: "确定".tr), barrierDismissible: false);
      }
    }else if (manager?.deviceModel?.name.startsWith("AX") == true){
      if (firmware <= 4.015){
        Get.dialog(AppDialog(title: "提示".tr, message: "请先升级固件".tr, submit: "确定".tr), barrierDismissible: false);
      }else{
        Get.dialog(AppDialog(title: "提示".tr, message: "请先移除充电线并重新连接".tr, submit: "确定".tr), barrierDismissible: false);
      }
    }
  }

  Future resetPowerOffset()async{

    try{
      loading(true);
      var hasOneSuccess = false;
      try{
        if (bleManager.isReady.value == true){
          await bleManager.resetPowerOffset();
          bleManager.refreshPowerOffset();
          hasOneSuccess = true;
        }
      }on EmptyWriteCharacteristicError catch(e){
        printError(info: "$e");
        _showNotSupport(bleManager);
      }catch(e){

      }
      try{
        if (rightBleManager?.isReady.value == true){
          await rightBleManager?.resetPowerOffset();
          rightBleManager?.refreshPowerOffset();
          hasOneSuccess = true;
        }
      }on EmptyWriteCharacteristicError catch(e){
        printError(info: "$e");
        _showNotSupport(rightBleManager);
      }catch(e){

      }
      if (!hasOneSuccess){
        if (await bleManager.ensureDeviceConnected()){
          // await resetPowerOffset();
        }
      }else{
        powerOffset(1000);
        success(true);
      }

    }catch(e){
      print("$e");
    }finally{
      loading(false);
    }
  }

}
